package com.weipai.demo.service.impl;

import com.weipai.demo.enums.RetCode;
import com.weipai.demo.mapper.base.PictureCategoryMapper;
import com.weipai.demo.mapper.base.PictureMapper;
import com.weipai.demo.model.Picture;
import com.weipai.demo.model.PictureCategory;
import com.weipai.demo.service.PictureService;
import com.weipai.demo.util.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: 赵项阳
 * @Date: 2019/6/14 15:55
 * @Version 1.0
 */
@Service
public class PictureServiceImpl implements PictureService  {
    private final Logger logger = LoggerFactory.getLogger(getClass());

     @Autowired
     private PictureCategoryMapper pictureCategoryMapper;
     @Autowired
     private PictureMapper pictureMapper;


    //展示轮播图的分类
    @Override
    public Response selectByShowClass(PictureCategory pictureCategory){
        Response response=new Response();
        List<PictureCategory> pictureCategoryList = pictureCategoryMapper.select(pictureCategory);
        response.put("pictureCategoryList",pictureCategoryList);
        response.setCode(RetCode.SUCCESS);
        return response;
    }


    //展示轮播图
    @Override
    public Response selectByShowPaging(Picture picture) {
        Response response= null;
        try {
            response = new Response();

            List<Picture> pictureList = pictureMapper.select(picture);
            response.put("pictureList",pictureList);
            response.setCode(RetCode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return response;
    }


    //删除轮播图
    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Response delePicture(Picture picture) {
        Response response=new Response();
        int delete = pictureMapper.delete(picture);
        response.setCode(RetCode.DELETESUCCESS);
        return response;
    }


    //保存图片
    @Override
    public Response savePicture(Picture picture) {
        Response response = new Response();
        if(picture.getPictureId()==null){
            pictureMapper.insert(picture);
            response.setCode(RetCode.SUCCESS);

        }else {
            int i = pictureMapper.updateByPrimaryKey(picture);
            if (i>0){
                response.setCode(RetCode.UPDATESUCCESS);
            }else {
                response.setCode(RetCode.UPDATEERROR);
                return response;
            }

        }



        return response;
    }

    //编辑轮播图
    @Override
    public Response editor(Picture picture) {
        Response response=new Response();
        int i = pictureMapper.updateByPrimaryKey(picture);
        if (i>0){
            response.setCode(RetCode.UPDATESUCCESS);
        }else {
            response.setCode(RetCode.UPDATEERROR);
            return response;
        }
        return response;
    }

    //APP展示广告图
    @Override
    public Response advertisingShow(Picture picture) {
        Response response= null;
        try {
            response = new Response();

            List<Picture> pictureList = pictureMapper.select(picture);
            response.put("pictureList",pictureList);
            response.setCode(RetCode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return response;
    }


}
